/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/testdriver/Screen2DTestDriver.h"

namespace alkes {

static IGraphics2D* s_graphics = 0;

Screen2DTestDriver::Screen2DTestDriver(IGraphics2D* graphics)
: graphics2d_(graphics)
, screen_(0)
{
    s_graphics = graphics;
}

Screen2DTestDriver::~Screen2DTestDriver()
{
    destroyScreen();
}

void Screen2DTestDriver::update()
{
    if (!screen_)
        return ;

    screen_->update();
}

void Screen2DTestDriver::render(const Rect* dirty_rect)
{
    if (!screen_)
        return ;

    screen_->render(dirty_rect);
}

void Screen2DTestDriver::beginTestCase1()
{
    destroyScreen();

    screen_ = constructTestCase1();
}

void Screen2DTestDriver::beginTestCase2()
{
    destroyScreen();

    screen_ = constructTestCase2();
}

class MyScene
    : public Scene
{
public:
    MyScene()
    {
    }

    virtual ~MyScene()
    {
    }

    virtual Composite* getRootNode()
    {
        return &root_clip_;
//        return this;
    }

    bool construct(IGraphics* graphics)
    {
        IGraphics2D* g = static_cast<IGraphics2D*>(graphics);
        root_clip_.setRect(URect(200, 100, 120, 120));
        root_clip_.addChild(this);
        root_clip_.disableClip(true);

        RectangleShapeNode::construct(rectangle1_, g, URect(0,0, 120,120), Color::BLUE);
        this->addChild(&rectangle1_);

        RectangleShapeNode::construct(rectangle2_, g, URect(40,40, 120,120), Color::RED);
        this->addChild(&rectangle2_);

        TextNode::construct(
            text1_, g, EmbedGlyphProvider::create(), L"ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n0123456789 0x0048fa00 32.0fps 55.1/100.0", Color::BLACK);
        this->addChild(&text1_);

        return true;
    }

protected:
    virtual void onDestroy()
    {
    }

private:
    ScissorClip root_clip_;
    RectangleShapeNode rectangle1_;
    RectangleShapeNode rectangle2_;
    TextNode text1_;
};


class MyScene2
    : public Scene
{
public:
    MyScene2()
    {
    }

    virtual ~MyScene2()
    {
    }

    virtual Composite* getRootNode()
    {
        return this;
    }

    bool construct(IGraphics* graphics)
    {
        IGraphics2D* g = static_cast<IGraphics2D*>(graphics);

        root_clip_.setRect(URect(50, 50, 120, 120));
        root_clip_.addChild(this);
        root_clip_.disableClip(true);

        RectangleShapeNode::construct(background_, g, URect(0,0, 300, 480), Color::SILVER);
        this->addChild(&background_);

        RectangleShapeNode::construct(rectangle1_, g, URect(0,0, 120,120), Color::BLUE);
        this->addChild(&rectangle1_);

        RectangleShapeNode::construct(rectangle2_, g, URect(40,40, 120,120), Color::RED);
        this->addChild(&rectangle2_);

        this->addChild(&image1_);
        image1_.setDest(UPoint2D(100, 100));
        ImageProvider provider(s_graphics->createDecoderFactory());
        image1_.setShape(s_graphics->createImageShape(provider.decodeImage("../data/pencil.png"), Point2D::ZERO));

        return true;
    }

protected:
    virtual void onDestroy()
    {
    }

private:
    ScissorClip root_clip_;
    RectangleShapeNode background_;
    RectangleShapeNode rectangle1_;
    RectangleShapeNode rectangle2_;
    ImageShapeNode image1_;
};

Screen2D* Screen2DTestDriver::constructTestCase1()
{
    Screen2D* screen = new Screen2D(graphics2d_);

    screen->initialize();
    screen->setClearColor(Color::WHITE);

    Layer* layer = screen->registerLayer();
//    layer->setLayerBlendMode(AL_LAYER_BLEND_NORMAL);

    layer->setSize(URectSize(640, 480));

    MyScene* scene = alkes_registerScene<MyScene>(layer);
    scene->construct(layer->getIGraphics2D());

    Layer* overlay_layer = screen->registerLayer();
    overlay_layer->setSize(URectSize(300, 480));
    overlay_layer->setLayerBlendMode(AL_LAYER_BLEND_NORMAL);
//    overlay_layer->setLayerBlendMode(AL_LAYER_BLEND_MULTIPLY);
//    overlay_layer->setLayerBlendMode(AL_LAYER_BLEND_SCREEN);
    overlay_layer->setOpacity(80);
    overlay_layer->setVisible(true);
    alkes_registerScene<MyScene2>(overlay_layer);

    AL_INFO("sizeof(UpdateContext) =%4d", sizeof(UpdateContext));
    AL_INFO("sizeof(Screen)   =%4d", sizeof(Screen));
    AL_INFO("sizeof(Screen2D) =%4d", sizeof(Screen2D));
    //AL_INFO("sizeof(Screen3D) =%4d", sizeof(Screen3D));
    AL_INFO("sizeof(Layer)    =%4d", sizeof(Layer));
    //AL_INFO("sizeof(Layer3D)  =%4d", sizeof(Layer3D));
    AL_INFO("sizeof(Scene)    =%4d", sizeof(Scene));
    AL_INFO("sizeof(MyScene)  =%4d", sizeof(MyScene));
    AL_INFO("sizeof(MyScene2) =%4d", sizeof(MyScene2));

    return screen;
}

Screen2D* Screen2DTestDriver::constructTestCase2()
{
    return 0;
}

void Screen2DTestDriver::destroyScreen()
{
    if (!screen_)
        return ;

    screen_->finalize();

    delete screen_;

    screen_ = 0;
}

}
